$(document).ready(function() {
	var lastUpdatedAt;
	var getLastUpdatedAt = function(){
		if(lastUpdatedAt){
			return lastUpdatedAt.toISOString();
		} else {
			return "";
		}
	};
    $('#select-services').each(function(){
    	var handleSuccess = function(result){
    		var $form = $("#select-services");
    		if (result.length > 0) {
        		$form.hide();
                var $select = $form.find("select");
                var $buttons = $("#service-buttons");
                
                $.each(result, function(){
                        var opt = $("<option></option>").attr("value", this["key"]).html(this["name"]);
                        var link = $("<a></a>").html(this["name"]).attr('href', "#").data("key", this["key"]).click(function(){
                        	$select.val($(this).data("key"));
                        	$form.submit();
                        	return false;
                        });
                        $buttons.prepend(link);
                        $select.append(opt);
                });    			
    		} else {
    			$form.closest('.service').hide();
    		}
    	}
        $.ajax({url: "/services", dataType: "json",
                success: handleSuccess}
        );
    });
    
    $("#more").click(function(){
    	$(this).parent().parent().find(":hidden").show().end().end().end().hide();
    	return false;
    });
    
    $('#news').each(function(){
    	var renderService = function(service){
    		var $service = $("#service_" + service["key"]);
    		var $contents;	
    		if ($service.size() == 0) {
    			$service = $("<div class='service' id='service_" + service["key"] + "'></div>");
    			var $serviceName = $("<div class='service-name'></div>").html(service["name"]);
    			$service.append($serviceName);
    			$contents = $('<div class="contents"></div>');
    			$service.append($contents);
    		} else {
    			$contents = $service.find(".contents");
    		}
			$.each(service["news"], function(){
				var $link = $('<a></a>').attr("target", "_blank").attr('href', this["targetUrl"]).html(this["title"] || this["createdAt"]);
				var $title = $('<div class="title"></div>').append($link);
				$contents.append($title);
				if(this["content"]) {
					var $content = $("<div class='content'></div>").html(this["content"]);
					$contents.append($content);
				}
			});
			
			return $service;
    	};
    	
    	var newsSuccessHandler = function(result){
			var $news = $("#news");
			$.each(result, function(){
				$news.prepend(renderService(this));
			});
			lastUpdatedAt = new Date();
		};
		
		var updateNews = function(){
			// console.log("Update news");
			// console.log("Last updated at: " + getLastUpdatedAt());
			$.ajax({
				url: "/news",
				dataType: "json",
				data: {since: getLastUpdatedAt()},
				success: newsSuccessHandler
			});
			setTimeout(updateNews, 60 * 1000);
		};
		updateNews();
    });
});

theObjects = document.getElementsByTagName("object");
for (var i = 0; i < theObjects.length; i++)
{
	theObjects[i].outerHTML = theObjects[i].outerHTML;
}

